/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Control.View;
import Model.Curve;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class NewClass extends JPanel{

    public NewClass(LinkedList ll,View a) {
   
      setBackground(Color.gray);         // Background color of applet, shows
                                         //   in borders and between components.
      setLayout(new BorderLayout(3,3));  // Layout manager for applet.
      
      SimplePaintCanvas3 canvas = new SimplePaintCanvas3(ll,a);  // The drawing area.
      add(canvas,BorderLayout.CENTER);
      

      
 
//      canvas.colorChoice = choice;    // Canvas needs access to the pop-up menus,
//      canvas.figureChoice = choice2;  //   so it can check it to find out what
                                      //   color and shape to use.
      
   }  // end init()
    @Override
    public Insets getInsets() {
          // Specify how wide a border to leave around the edges of the applet.
      return new Insets(3,3,3,3);
   }
}

class SimplePaintCanvas3 extends Canvas 
                  {
    View view;
    Image OSC;  // The off-screen canvas (created in setupOSC()).
    LinkedList<Object> ll;
   int widthOfOSC, heightOfOSC;  // Current widht and height of OSC.  These
                                 // are checked against the size of the applet,
                                 // to detect any change in the applet's size.
                                 // If the size has changed, a new OSC is created.
                                 // The picture in the off-screen canvas is lost
                                 // when that happens.
   

   /* The following variables are used when the user is sketching a
      curve while dragging a mouse. */

   private int prevX, prevY;     // The previous location of the mouse.
   
   private int startX, startY;   // The starting position of the mouse.
                                 // (Not used for drawing curves.)
   
   private boolean dragging;     // This is set to true when the user is drawing.
   
   private int figure;    // What type of figure is being drawn.  This is
                          //    specified by the figureChoice menu.
   
   private Graphics graphicsForDrawing;  // A graphics context for the applet.
   
   private Graphics offscreenGraphics;   // A graphics context for the off-screen canvas.
                                         

   SimplePaintCanvas3(LinkedList ll1,View a) {
          // Constructor.  When the canvas is first created, it is set to
          // listen for mouse events and mouse motion events from
          // itself.  The initial background color is white.
      
      
      setBackground(Color.white);
      this.ll=ll1;
      this.view=a;
   }
   
 
   private void setupOSC1() {
        // This method is responsible for creating the off-screen canvas. 
        // It should be called before using the OSC.  It will make a new OSC if
        // the size of the applet changes.
      if (OSC == null || widthOfOSC != getSize().width || heightOfOSC != getSize().height) {
             // Create the OSC, or make a new one if applet size has changed.
         OSC = null;  // (If OSC already exists, this frees up the memory.)
         OSC = createImage(getSize().width, getSize().height);
         widthOfOSC = getSize().width;
         heightOfOSC = getSize().height;
         Graphics OSG = OSC.getGraphics();  // Graphics context for drawing to OSC.
         OSG.setColor(getBackground());
         OSG.fillRect(0, 0, widthOfOSC, heightOfOSC);
         
      }
   }
   private Graphics getOSG() {
         // Return a graphics context for drawing to the off-screen canvas.
         // A new canvas is created if necessary.  Note that the graphics
         // context should not be kept for any length of time, in case the
         // size of the canvas changes.
       setupOSC1();
       return OSC.getGraphics();
   }

/*LinkedList<BufferedImage> arr=new LinkedList<BufferedImage>();*/
   private void clearOSC() {
         // Fill the off-screen canvas with the background color.
       
       Graphics OSG = OSC.getGraphics();
       OSG.setColor(getBackground());
       OSG.fillRect(0, 0, widthOfOSC, heightOfOSC);
       OSG.dispose();
   }

    @Override
   public void update(Graphics g) {
       
         // Redefine update so it doesn't clear the canvas before calling paint().
      paint(g);
   }
   
    @Override
   public void paint(Graphics g) {
       setupOSC1();
      g.drawImage(OSC, 0, 0, this);
      
      for(int i =0;i<ll.size();i++)
            {
                Graphics g1=OSC.getGraphics();
                ((Shape)ll.get(i)).drawShape1(g,50,OSC,this);
                ((Shape)ll.get(i)).drawShape(g1,(BufferedImage)OSC);
                try {
                                Thread.sleep(50);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(Curve.class.getName()).log(Level.SEVERE, null, ex);
                            }
            }
      
//      Image temp=this.createImage(this.getWidth(), this.getHeight());
//        try {
//            ImageIO.write((BufferedImage)temp, "jpg", new File("xc.jpg"));
//        } catch (IOException ex) {
//            Logger.getLogger(Oval.class.getName()).log(Level.SEVERE, null, ex);
//        }
        
        
//      ((Shape)ll.get(0)).drawShape(g,(BufferedImage)OSC);
//                if(ll.size()==2)
//                ((Shape)ll.get(1)).drawShape(g,(BufferedImage)OSC);
//      g.dispose();
   }
}

